package com.mlm.mlmapp.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.WindowManager;

import com.mlm.mlmapp.BaseApplication;
import com.mlm.mlmapp.cons.HTTPCons;
import com.mlm.mlmapp.utils.CommonUtils;

import org.json.JSONArray;
import org.json.JSONObject;

public class BaseActivity extends FragmentActivity implements HTTPCons {

    public final static int FLAG_USER_INFO = 0;
    public final static int FLAG_APPOINTMENT = 1;
    public final static int FLAG_COUPON = 2;
    public static final int AN_REQUEST_CODE = 10;
    public static final int REQUEST_CODE = 11;
    public static final int AVATAR_REQUEST_CODE = 12;
    public static final int PHOTO_PICKED_WITH_DATA = 13;
    public static final int GALLERY_AVATAR_REQUEST_CODE = 14;
    public static final int LOGIN_FORWARD = 15;
    public static final int REQUEST_CODE_PAYMENT = 114;
    public static final int REQUEST_CODE_DATE = 111;
    public static final int REQUEST_CODE_COMBO = 112;
    public static final int REQUEST_CODE_DRESSER = 113;
    public static final int REQUEST_CODE_VALID_COUPON = 1111;

    public void test() {


    }

    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);

    }

    protected void setTitle(Context context) {

    }

    protected void startAct(Activity act, Class target) {
        Intent intent = new Intent(act, target);
        startActivity(intent);
    }

    protected String getJsonString(JSONObject obj, String key) {
        return CommonUtils.getJsonString(obj, key);
    }

    protected JSONObject getJsonObject(JSONObject obj, String key) {
        return CommonUtils.getJsonObject(obj, key);
    }

    protected int getJsonInt(JSONObject obj, String key) {

        return CommonUtils.getJsonInt(obj, key);
    }

    protected JSONArray getJsonArray(JSONObject obj, String key) {
        return CommonUtils.getJsonArray(obj, key);
    }

    protected void setTranStatusBar() {
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            SystemBarTintManager tintManager = new SystemBarTintManager(this);
//            tintManager.setStatusBarTintEnabled(true);
//            tintManager.setStatusBarTintResource(android.R.color.transparent);
//			getWindow().addFlags(
//					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    public int dp2px(int dpValue) {
        return (int) (dpValue * getResources().getDisplayMetrics().density);
    }

    public boolean haveNetworkConnection() {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();

        for (NetworkInfo ni : netInfo) {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected())
                    haveConnectedWifi = true;
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected())
                    haveConnectedMobile = true;
        }
        return haveConnectedWifi || haveConnectedMobile;
    }

    public void finishApplication() {
        for (Class c : BaseApplication.map.keySet()) {
            BaseApplication.map.get(c).finish();
        }
    }
}
